package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.config.ServerConfig;
import com.ruoyi.system.domain.SysCheck;
import com.ruoyi.system.domain.SysContractMoney;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysBalancePayment;
import com.ruoyi.system.service.ISysBalancePaymentService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 【尾款支付】Controller
 * 
 * @author zhaoguanyu
 * @date 2022-01-20
 */
@Api(tags = "尾款支付")
@RestController
@RequestMapping("/system/payment")
public class SysBalancePaymentController extends BaseController
{
    @Autowired
    private ISysBalancePaymentService sysBalancePaymentService;
    @Autowired
    private ServerConfig serverConfig;

    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('lifecycle:finalPay:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysBalancePayment sysBalancePayment)
    {
        startPage();
        List<SysBalancePayment> list = sysBalancePaymentService.selectSysBalancePaymentList(sysBalancePayment);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('lifecycle:finalPay:export')")
    @Log(title = "【尾款支付】", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysBalancePayment sysBalancePayment)
    {
        List<SysBalancePayment> list = sysBalancePaymentService.selectSysBalancePaymentList(sysBalancePayment);
        ExcelUtil<SysBalancePayment> util = new ExcelUtil<SysBalancePayment>(SysBalancePayment.class);
        return util.exportExcel(list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize("@ss.hasPermi('lifecycle:finalPay:query')")
    @GetMapping(value = "/{itemNo}")
    public AjaxResult getInfo(@PathVariable("itemNo") String itemNo)
    {
        return AjaxResult.success(sysBalancePaymentService.selectSysBalancePaymentById(itemNo));
    }

    /**
     * 新增【付款申请】
     */
    @PreAuthorize("@ss.hasPermi('lifecycle:finalPay:add')")
    @Log(title = "【新增付款申请】", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody SysBalancePayment sysBalancePayment)
    {
        return toAjax(sysBalancePaymentService.insertSysBalancePayment(sysBalancePayment));
    }

    /**
     *提交项目合同
     * @Author zhaoguanyu
     * @Date 13:59 2021/12/21
     */
    @ApiOperation(value = "提交项目合同文件")
    @PreAuthorize("@ss.hasPermi('lifecycle:finalPay:submit')")
    @Log(title = "尾款支付", businessType = BusinessType.IMPORT)
    @PostMapping("/upload")
    public AjaxResult uploadFile(MultipartFile file) throws Exception
    {
        try
        {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            String url = serverConfig.getUrl() + fileName;
            AjaxResult ajax = AjaxResult.success();
            //文件中文名称
            String name = file.getOriginalFilename();
            SysBalancePayment sysBalancePayment = new SysBalancePayment();
            sysBalancePayment.setFileName(name);
            sysBalancePayment.setUrl(url);
            sysBalancePayment.setOnlyName(fileName);
            Long id = sysBalancePaymentService.insertFileUpload(sysBalancePayment);
            ajax.put("id",id);
            ajax.put("fileName", fileName);
            ajax.put("url", url);
            return ajax;
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 审批【付款申请】
     */
    @PreAuthorize("@ss.hasPermi('lifecycle:finalPay:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/{itemNo}")
    public AjaxResult edit(@PathVariable("itemNo") String itemNo)
    {
        return toAjax(sysBalancePaymentService.updateSysBalancePayment(itemNo));
    }

    /**
     * 新增【确认支付】
     */
    @PreAuthorize("@ss.hasPermi('lifecycle:finalPay:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping("/addpay")
    public AjaxResult addpay(@RequestBody SysBalancePayment sysBalancePayment)
    {
        return toAjax(sysBalancePaymentService.addSysBalancePayment(sysBalancePayment));
    }

    /**
     *提交支付凭证图片
     * @Author zhaoguanyu
     * @Date 13:59 2021/1/20
     */
    @ApiOperation(value = "提交验收报告")
    @PreAuthorize("@ss.hasPermi('lifecycle:finalPay:submit')")
    @Log(title = "验收阶段", businessType = BusinessType.IMPORT)
    @PostMapping("/uploadPicture")
    public AjaxResult uploadPicture(MultipartFile file) throws Exception
    {
        try
        {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String name = file.getOriginalFilename();
            String fileName = FileUploadUtils.upload(filePath, file);
            String url = serverConfig.getUrl() + fileName;
            AjaxResult ajax = AjaxResult.success();
            SysBalancePayment sysBalancePayment = new SysBalancePayment();
            sysBalancePayment.setpName(name);
            sysBalancePayment.setpOnlyName(fileName);
            sysBalancePayment.setpUrl(url);
            Long id = sysBalancePaymentService.uploadPicture(sysBalancePayment);
            ajax.put("id",id);
            ajax.put("url", url);
            return ajax;
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

}
